﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
using System.Security.Cryptography;

namespace Cheems.Control.Common
{
    /// <summary>
    /// 字符串转哈希头像
    /// </summary>
    public class IdenticonGenerator
    {
        public static ImageSource Generate(string input, int size = 400)
        {
            byte[] hash = ComputeHash(input);
            Color foregroundColor = GetColor(hash);
            bool[,] grid = CreateGrid(hash);

            DrawingVisual visual = new DrawingVisual();
            using (DrawingContext dc = visual.RenderOpen())
            {
                int cellSize = size / 5;
                for (int row = 0; row < 5; row++)
                {
                    for (int col = 0; col < 5; col++)
                    {
                        if (grid[row, col])
                        {
                            Rect rect = new Rect(
                                col * cellSize,
                                row * cellSize,
                                cellSize,
                                cellSize
                            );
                            dc.DrawRectangle(
                                new SolidColorBrush(foregroundColor),
                                null,
                                rect
                            );
                        }
                    }
                }
            }

            // Convert RenderTargetBitmap to BitmapImage
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
                size, size, 96, 96, PixelFormats.Pbgra32);
            renderTargetBitmap.Render(visual);

            BitmapImage bitmapImage = new BitmapImage();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // Encode RenderTargetBitmap to a stream
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
                encoder.Save(memoryStream);
                memoryStream.Seek(0, SeekOrigin.Begin);

                // Load the BitmapImage from the memory stream
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
            }

            return bitmapImage;
        }

        private static byte[] ComputeHash(string input)
        {
            using (SHA1 sha1 = SHA1.Create())
            {
                return sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
            }
        }

        private static Color GetColor(byte[] hash)
        {
            return Color.FromRgb(hash[0], hash[1], hash[2]);
        }

        private static bool[,] CreateGrid(byte[] hash)
        {
            bool[,] grid = new bool[5, 5];
            BitArray bits = new BitArray(hash);

            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 3; col++)
                {
                    int bitIndex = row * 3 + col;
                    bool value = bits.Length > bitIndex && bits[bitIndex];

                    grid[row, col] = value;
                    if (col < 2) // Mirror first two columns to the right
                    {
                        grid[row, 4 - col] = value;
                    }
                }
            }

            return grid;
        }
    }
}
